package com.expand;

import com.handmark.pulltorefresh.library.R;

import android.annotation.SuppressLint;
import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.Scroller;
import android.widget.TextView;

public class SlideView extends LinearLayout {

	private static final String TAG = "SlideView";

	public static final int SLIDE_STATUS_OFF = 0;
	public static final int SLIDE_STATUS_START_SCROLL = 1;
	public static final int SLIDE_STATUS_ON = 2;

	private Context mContext;
	private LinearLayout ll_slide_note;
	private RelativeLayout mHolder;
	private Scroller mScroller;
	private OnSlideListener mOnSlideListener;

	private int mHolderWidth = 75;

	private int mLastX = 0;
	private int mLastY = 0;
	private static final int TAN = 2;
	private int currStatus = SLIDE_STATUS_OFF;

	private RelativeLayout rl_delete = null;

	public interface OnSlideListener {

		/**
		 * @param view
		 *            current SlideView
		 * @param status
		 *            SLIDE_STATUS_ON or SLIDE_STATUS_OFF
		 */
		public void onSlide(View view, int status);

		public void onSlideClick(View view);
	}

	public SlideView(Context context) {
		super(context);
		initView();
	}

	public SlideView(Context context, AttributeSet attrs) {
		super(context, attrs);
		initView();
	}

	private void initView() {
		deltaX = 0;
		isMoveRight = false;
		mContext = getContext();
		mScroller = new Scroller(mContext);
		setOrientation(LinearLayout.HORIZONTAL);
		View.inflate(mContext, R.layout.slide_view_merge, this);
		ll_slide_note = (LinearLayout) findViewById(R.id.ll_slide_note);
		rl_delete = ((RelativeLayout) findViewById(R.id.rl_delete));
		mHolderWidth = mContext.getResources().getDimensionPixelOffset(R.dimen.btn_delete_width);
		// mHolderWidth = Math.round(TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, mHolderWidth, getResources().getDisplayMetrics()));
	}

	public void setButtonText(CharSequence text) {
		((TextView) findViewById(R.id.tv_delete)).setText(text);
	}

	/** 获取删除控件 */
	public RelativeLayout getDeleteView() {
		return rl_delete;
	}

	/** 获取内容事件控件 */
	public LinearLayout getSlideNote() {
		return ll_slide_note;
	}

	public void setContentView(View view) {
		ll_slide_note.addView(view);
	}

	public void setOnSlideListener(OnSlideListener onSlideListener) {
		mOnSlideListener = onSlideListener;
	}

	public void shrink() {
		if (getScrollX() != 0) {
			Log.i(TAG, "shrilk instance==" + this);
			this.smoothScrollTo(0, 0);
		}
		currStatus = SLIDE_STATUS_OFF;
	}

	private int deltaX;

	private boolean isMoveRight = false;

	@SuppressLint("NewApi")
	public void onRequireTouchEvent(MotionEvent event) {
		int x = (int) event.getX();
		int y = (int) event.getY();
		int scrollX = getScrollX();

		switch (event.getAction()) {
		case MotionEvent.ACTION_DOWN: {
			if (!mScroller.isFinished()) {
				mScroller.abortAnimation();
			}
			if (mOnSlideListener != null) {
				mOnSlideListener.onSlide(this, SLIDE_STATUS_START_SCROLL);
			}
			isMoveRight = false;
			break;
		}
		case MotionEvent.ACTION_MOVE: {
			deltaX = x - mLastX;
			int deltaY = y - mLastY;
//			if (Math.abs(deltaX) < Math.abs(deltaY) * TAN) {
//				isMoveRight = true;
//				break;
//			}

			int newScrollXMove = scrollX - deltaX;
			if (deltaX != 0) {
				if (newScrollXMove < -mHolderWidth / 2) {
					// 手指滑动方向向右
					isMoveRight = true;
					newScrollXMove = 0;
				} else if (newScrollXMove > mHolderWidth) {
					newScrollXMove = mHolderWidth;
				}
				this.scrollTo(newScrollXMove, 0);
			}
			break;
		}
		case MotionEvent.ACTION_UP: {
			int newScrollX = 0;
			// 移动的距离超过删除按钮1\2宽度时，开关打开、平滑移动到200；否则开关关闭、平滑移动到0
			if (scrollX - mHolderWidth * 0.5 > 0) {
				newScrollX = mHolderWidth;
			}
			this.smoothScrollTo(newScrollX, 0);
			Log.i(TAG, "original currStatus==" + currStatus);
			// 原来开关状态显示时，点击隐藏开关，这是不再需要通过newScrollX来判断开关状态
			if ((currStatus == SLIDE_STATUS_ON || currStatus == SLIDE_STATUS_START_SCROLL) && deltaX < mHolderWidth * 0.5) {
				Log.i(TAG, "do shrink currStatus==0");
				shrink();
			} else {
				currStatus = newScrollX == 0 ? SLIDE_STATUS_OFF : SLIDE_STATUS_ON;
				Log.i(TAG, "set currStatus==" + currStatus + ",deltaX==" + deltaX);
			}

			if (mOnSlideListener != null) {
				mOnSlideListener.onSlide(this, currStatus);
			}

			Log.i(TAG, "newScrollX==" + newScrollX + ",isMoveRight==" + isMoveRight);
			if (currStatus == SLIDE_STATUS_OFF && newScrollX == 0 && !isMoveRight) {
				if (mOnSlideListener != null) {
					mOnSlideListener.onSlideClick(this);
				}
				Log.i(TAG, "mOnSlideListener.onSlideClick(this)");
			}
			break;
		}
		default:
			break;
		}

		mLastX = x;
		mLastY = y;
	}

	private void smoothScrollTo(int destX, int destY) {
		// 缂撴參婊氬姩鍒版寚瀹氫綅缃�
		int scrollX = getScrollX();
		int delta = destX - scrollX;
		mScroller.startScroll(scrollX, 0, delta, 0, Math.abs(delta) * 3);
		invalidate();
	}

	public int getCurrStatus() {
		return currStatus;
	}

	@Override
	public void computeScroll() {
		if (mScroller.computeScrollOffset()) {
			scrollTo(mScroller.getCurrX(), mScroller.getCurrY());
			postInvalidate();
		}
	}

}
